import os
import time
import random
import sys

def clear_screen():
    """清除控制台屏幕"""
    os.system('cls' if os.name == 'nt' else 'clear')

def print_smiley(position, height, face):
    """在指定位置打印笑脸"""
    # 打印空白行
    for _ in range(position):
        print()
    # 打印笑脸
    print(face)
    # 打印剩余的空白行
    for _ in range(height - position - 1):
        print()

def get_random_face():
    """随机生成不同的笑脸表情"""
    faces = [
        "  ^_^  ",
        "  >_<  ",
        "  o_o  ",
        "  -_-  ",
        "  *_*  ",
        "  >.<  ",
        "  ^o^  "
    ]
    return random.choice(faces)

def main():
    try:
        # 设置动画参数
        screen_height = 15
        position = 0
        direction = 1  # 1 表示向下，-1 表示向上
        speed = 0.15
        face = get_random_face()
        face_change_counter = 0
        
        print("跳动的笑脸！按 Ctrl+C 退出")
        time.sleep(2)
        
        while True:
            clear_screen()
            
            # 打印当前帧
            print_smiley(position, screen_height, face)
            
            # 更新位置
            position += direction
            
            # 碰到边界反弹
            if position >= screen_height - 1:
                direction = -1
                face_change_counter += 1
            if position <= 0:
                direction = 1
                face_change_counter += 1
            
            
            # 偶尔改变表情
            if face_change_counter >= 3:
                face = get_random_face()
                face_change_counter = 0
            
            # 控制速度
            time.sleep(speed)
            
    except KeyboardInterrupt:
        clear_screen()
        print("谢谢观看！")

if __name__ == "__main__":
    main()
